package main.java.LeetCode.ClassicalAlgorithm;



import main.java.LeetCode.ClassicalAlgorithm.Queue.Impl.BlockQueue;
import main.java.LeetCode.ClassicalAlgorithm.Queue.Impl.BlockQueueGroup;
import main.java.LeetCode.ClassicalAlgorithm.Queue.Impl.LinkedBlockQueue;
import main.java.LeetCode.ClassicalAlgorithm.Worker.Impl.task.DataTask;
import main.java.LeetCode.ClassicalAlgorithm.Worker.Impl.event.EventLoopGroup;
import main.java.LeetCode.ClassicalAlgorithm.Worker.Task;


import java.util.*;
import java.util.concurrent.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner in = new Scanner(System.in);
        CountDownLatch countDownLatch = new CountDownLatch(4);

        BlockQueueGroup<Task> queueGroup = new BlockQueueGroup<>(4,50);
        BlockQueue<Task> blockQueue = new BlockQueue<>(200);
        LinkedBlockQueue<Task> linkedBlockQueue = new LinkedBlockQueue<Task>(300);

        Object object = new Object();

        ArrayList<Integer> listF = new ArrayList<>();
        ArrayList<Integer> listT = new ArrayList<>();

        EventLoopGroup eventLoopGroup = new EventLoopGroup(5, 10,queueGroup,10,TimeUnit.SECONDS,10);

        long time = System.currentTimeMillis();

        for (int i = 0; i < 4; i++) {
            new Thread(() -> {
                int k = 0;
                int f = 0;
                int t = 0;
                while (k<4000) {
                    if(eventLoopGroup.submit(new DataTask()))
                        t++;
                    else
                        f++;
                    k++;
                }


                synchronized (object){
                    listT.add(t);
                    listF.add(f);
                }
                countDownLatch.countDown();
            }).start();

        }

        countDownLatch.await();

        long etime = System.currentTimeMillis();
        System.out.printf("执行时长：%d 毫秒.", (etime - time));
        System.out.println();

        float sumF = 0;
        float sumT = 0;

        for(int i = 0;i<listT.size();i++){
            sumT = sumT+listT.get(i);
            sumF = sumF+listF.get(i);
        }

        System.out.println("False: "+sumF+" True: "+sumT);
        System.out.println("False: "+(sumF/(sumF+sumT))*100+"%");
        System.out.println("True: "+(sumT/(sumF+sumT))*100+"%");
        eventLoopGroup.shutdown();

    }
}
